import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import '../app/di.dart';
import '../domain/counter/counter_store.dart';
import 'package:provider/provider.dart';
import '../log/logger.dart';
import '../http/api.dart';
import '../utils/toast.dart';
import '../utils/click.dart';

class CounterPage extends StatelessWidget {
  const CounterPage({super.key});

  @override
  Widget build(BuildContext context) {
    final counterStore = getIt<CounterStore>();
    // final logger = Provider.of<Logger>(context);
    final logger = getIt<AppLogger>().instance;
    final api = getIt<Api>();

    return PopScope(
        onPopInvokedWithResult: (didPop, result) async {
          if (!didPop) {
            bool isExit = await ClickUtils.exitBy2Click(
                duration: 1000, status: Scaffold.of(context));
            if (isExit) {
              Navigator.of(context).pop();
            }
          }
          return;
        },
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Counter Page'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text('You have pushed the button this many times:'),
                Observer(
                  builder: (_) => Text(
                    '${counterStore.counter}',
                    style: Theme.of(context).textTheme.headlineMedium,
                  ),
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () async {
              var res = await api.getCurrentTime('B81332678ACF');
              ToastUtils.toast('Response:====>' + res.data.toString());
              logger.d('Response:====>' + res.data.toString());
              counterStore.increment();
              logger.d('Incrementing counter');
            },
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
        ));
  }
}
